﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.ServiceModel.DomainServices.Client;
using BadJokeProject.Web;

namespace BadJokeProject
{
    public partial class Verify : UserControl
    {
        private Settings parent;
        private const string Available = "/BadJokeProject;component/Awake.png";
        private const string Invalid = "/BadJokeProject;component/Invisible.png";
        private const string MouseOver = "/BadJokeProject;component/Alert4.png";
        private const string Pressed = "/BadJokeProject;component/Idle.png";
        private BadJokeDomainContext context = new BadJokeDomainContext();

        public Verify()
        {
            InitializeComponent();
            this.Img.Source = new BitmapImage(new Uri(Available, UriKind.RelativeOrAbsolute));
        }

        public void SetParentWindow(Settings window)
        {
            this.parent = window;
        }

        private void Img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Notify.Visibility = System.Windows.Visibility.Collapsed;
            this.Img.Source = new BitmapImage(new Uri(Invalid, UriKind.RelativeOrAbsolute));
            this.Img.IsHitTestVisible = false;

            InvokeOperation<bool> invoke = context.VerifyXiaoBai(this.Name.Text, this.Password.Text);
            invoke.Completed += (s, args) =>
                {
                    if (invoke.Error != null || !invoke.Value)
                    {
                        this.Notify.Visibility = System.Windows.Visibility.Visible;
                        this.Img.Source = new BitmapImage(new Uri(Available, UriKind.RelativeOrAbsolute));
                        this.Img.IsHitTestVisible = true;
                        return;
                    }

                    this.parent.NavigateToEditor();
                };
        }

        private void Img_MouseEnter(object sender, MouseEventArgs e)
        {
            this.Img.Source = new BitmapImage(new Uri(MouseOver, UriKind.RelativeOrAbsolute));
        }

        private void Img_MouseLeave(object sender, MouseEventArgs e)
        {
            this.Img.Source = new BitmapImage(new Uri(Available, UriKind.RelativeOrAbsolute));
        }
    }
}
